package com.pangyangyang.elasticsearch.plugin.jdbc.handler.action;

import com.pangyangyang.elasticsearch.plugin.event.DataModifyEvent;
import com.pangyangyang.elasticsearch.plugin.jdbc.query.QueryDataEntity;
import com.pangyangyang.elasticsearch.plugin.jdbc.query.QueryStatementExecutor;
import com.pangyangyang.elasticsearch.plugin.jdbc.handler.JDBCQueryHandler;
import com.pangyangyang.elasticsearch.plugin.river.Build;
import com.pangyangyang.elasticsearch.plugin.river.MessageRiverSettings;
import com.pangyangyang.elasticsearch.plugin.util.MessageRiverConstants;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by yangwang on 2015/1/22.
 */
public class JDBCQueryHandlerAction {

    private String eventMap;
    private JDBCQueryHandlerActionType action;
    private String index, type;
    private QueryStatementExecutor statementExecutor;
    private JDBCQueryHandler parentHandler;


    public JDBCQueryHandlerAction(String eventMap, MessageRiverSettings handlerSettings, JDBCQueryHandler parentHandler) {
        this.parentHandler = parentHandler;
        this.eventMap = eventMap;
        MessageRiverSettings actionSettings = new MessageRiverSettings((Map<String, Object>) handlerSettings.getObject(eventMap));

        statementExecutor = new QueryStatementExecutor(actionSettings);
        this.index = actionSettings.getString(MessageRiverConstants.SETTING_KEY_COMMON_INDEX);
        this.type = actionSettings.getString(MessageRiverConstants.SETTING_KEY_COMMON_TYPE);
        this.action = JDBCQueryHandlerActionType.fromString(actionSettings.getString(MessageRiverConstants.SETTING_KEY_HANDLER_JDBC_ACTION));
    }

    public boolean execute(DataModifyEvent event) {

        switch (action){
            case INDEX:
                List<QueryDataEntity> dataArray = this.statementExecutor.executeQuery(event.getData());
                if (dataArray.isEmpty()) {
                    return false;
                }
                for (QueryDataEntity entity : dataArray) {
                    if (StringUtils.isBlank(index) || StringUtils.isBlank(type)) {
                       return parentHandler.indexDocument(entity);
                    } else {
                        return parentHandler.indexDocument(index, type, entity);
                    }
                }
                break;
            case DELETE:
                List<String> idArray = this.statementExecutor.executeQueryIdOnly(event.getData());
                if (idArray.isEmpty()) {
                    return false;
                }
                for (String id : idArray) {
                    if (StringUtils.isBlank(index) || StringUtils.isBlank(type)) {
                        return parentHandler.deleteDocument(id);
                    } else {
                        return parentHandler.deleteDocument(index, type, id);
                    }
                }
                break;
        }

        return false;
    }

    public String getEventMap() {
        return eventMap;
    }

    public void start(){
        this.statementExecutor.start();
    }

    public void stop(){
        this.statementExecutor.stop();
    }

    @Override
    public String toString() {
        return "JDBCQueryHandlerAction{" +
                "statementExecutor=" + statementExecutor +
                ", index='" + index + '\'' +
                ", type='" + type + '\'' +
                ", eventMap='" + eventMap + '\'' +
                ", action=" + action +
                '}';
    }
}
